#!/bin/bash


echo "Executing script by: HUANGXIAOLEI 24055060326"

echo "-----------------------------------------"

MY_GENDER="F"

SOURCE_DIR="students"


if [ ! -d "$SOURCE_DIR" ]; then

    echo "Error: Directory '$SOURCE_DIR' not found!"

    exit 1

fi


cd "$SOURCE_DIR"


total_average_score=0.0

student_count=0



echo "Calculating average score for all '$MY_GENDER' students..."



if [ "$MY_GENDER" = "M" ]; then

    gender_full_name="male(M)"

else

    gender_full_name="female(F)"

fi



for file in *.txt; do



    gender=$(grep "gender:" "$file" | awk '{print $2}')

    

    if [ "$gender" = "$MY_GENDER" ]; then



        average_score=$(grep "average:" "$file" | awk '{print $2}')

        

        total_average_score=$(echo "$total_average_score + $average_score" | bc)

        student_count=$((student_count + 1))

        

        echo "Found $gender student: $(basename "$file" .txt) - average: $average_score"

    fi  

done

if [ $student_count -gt 0 ]; then

    final_average=$(echo "scale=2; $total_average_score / $student_count" | bc)

    echo "-----------------------------------------"

    echo "Total students found: $student_count"

    echo "Average score of $gender_full_name: $final_average"

else

    echo "-----------------------------------------"

    echo "No $gender_full_name students found!"

fi
